package jdbc.datasource;

import jdbc.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JDBCUtilsByDruid_USE {
    //使用druid工具类
    public static void main(String[] args) {
        System.out.println("使用 druid方式完成");
        String sql = "select * from admin where NAME = ?";
        Connection connection = null;
        ResultSet resultSet = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = JDBCUtils.getConnection();
            System.out.println(connection.getClass());//运行类型 com.alibaba.druid.pool.DruidPooledConnection
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,"admin");
            //执行, 得到结果集
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                String name = resultSet.getString("NAME");
                String pwd = resultSet.getString("pwd");
                System.out.println(name+"\t"+pwd);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            //关闭资源
            JDBCUtilsByDruid.close(resultSet,connection,preparedStatement);
        }
    }
}
